package com.skillworld.webapp.model.item;

import java.util.Locale;
import java.util.Map;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.skillworld.webapp.model.user.User;

@Entity
@org.hibernate.annotations.BatchSize(size=10)
@Table(name="Item")
public class Item 
{
    public static final String CASINO_ITEM_CODE = "CASINO";

	private Long itemId;
	private String code;
	private String image;  
	private int uses;
	private int lockLevel;
	private Map<Locale,ItemDetails> localizedDetails;
	
	
	protected Item()
	{

    }

	
    @SequenceGenerator(name="ItemIdGenerator",sequenceName="ItemSeq")
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO,generator="ItemIdGenerator")
	@Column(name="idItem")
	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}
	
	@Column(name="code")
	public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name="img")	
	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	@Column(name="lockLevel")
	public int getLockLevel() {
		return lockLevel;
	}

	public void setLockLevel(int lockLevel) {
		this.lockLevel = lockLevel;
	}
	
	/*
	 * Checks if the item is blocked for the
	 * given user.
	 */
	@Transient
	public boolean isBlockedFor(User user)
	{
	    return user.getGlobalLevel() < getLockLevel();
	}

	@Column(name="uses")
	public int getUses() {
		return uses;
	}

	public void setUses(int uses) {
		this.uses = uses;
	}

    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name="ItemDetails",
                     joinColumns=@JoinColumn(name="idItem"))
    @MapKeyColumn(name = "locale")
    protected Map<Locale,ItemDetails> getLocalizedDetails() {
        return localizedDetails;
    }

    protected void setLocalizedDetails(Map<Locale,ItemDetails> localizedDetails) {
        this.localizedDetails = localizedDetails;
    }
    
    @Transient
    public ItemDetails getDetails(Locale locale)
    {
        return this.getLocalizedDetails().get(locale);
    }
    
    @Transient
    public String getName(Locale locale)
    {
        return this.getDetails(locale).getName();
    }
    
    @Transient
    public String getInfo(Locale locale)
    {
        return this.getDetails(locale).getInfo();
    }
    
}
